﻿using System;
using System.Data;
using System.Web.Services;
using System.Web.UI;
using ZHDJ.Data.Models;
using ZHDJ.WebPage;
using FineUI;
using SysManage.BLL;
using ZHDJ.Core;
using ZHDJ.Core.Util;
using ZHDJ.Data.Models.Common;
using System.Web.UI;

namespace ZHDJ.Web.Portal
{
    public partial class CameraAutoHeight : DBPageBase
    {

        public bool IsAjax
        {
            get
            {
                var temp = Request["IsAjax"] ?? "";
                return temp == "1";
            }
        }
        public string BranchID
        {
            get
            {
                return Request["BranchID"] ?? "";
               
            }
        }

        public string UserBranchID { get; set; }
        public static bool IsHavePower()
        {
            return partyvideoprobe.IsHavePower(CurrentUserManager.CurrentUser.LoginID);
            
        }

        public static string ipType { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsAjax)
            {

                GetCameraUrl(BranchID);
            }
            else
            {
                CheckLogin(true, true);
                UserBranchID = CurrentUser.BranchID;

                if (this.CurrentUser.LoginID != "admin")
                {
                    if (!IsHavePower())
                    {
                        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('您无权访问此页面！');window.location.href='../default.aspx';", true);
                    }
                }
                if (!IsPostBack)
                {
                    //ipType = Request.UserHostAddress;
                    ipType = IPNetworking.GetIP4Address();
                    //ipType = Request.ServerVariables["REMOTE_ADR"].ToString();
                }
            }
        }

       
        public  void GetCameraUrl(string branchId)
        {
            if (!IsLogin)
            {
                Response.Clear();
                Response.Write("");
                Response.End();
            }
            var rs = "";
            DataTable dt = partyvideoprobe.GetCameraAddress(branchId, ipType);

            if (dt.Rows.Count == 0)
            {
                rs= "";
            }
            else
            {
                if (!string.IsNullOrEmpty(Convert.ToString(dt.Rows[0]["Port"])))
                {
                    rs= Convert.ToString(dt.Rows[0]["Address"]) + ":" + Convert.ToString(dt.Rows[0]["Port"]);
                }
                else
                {
                    rs= Convert.ToString(dt.Rows[0]["Address"]);
                }
            }
            rs = rs.ToLower().Contains("http://") ? rs : "http://" + rs;
            Response.Clear();
            Response.Write(rs);
            Response.End();
        }
    }
}